package com.chhuang.common;

import java.io.InputStream;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.sql.DataSource;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import com.mchange.v2.c3p0.ComboPooledDataSource;

/**
 * 数据库连接工具类
 * @author HuangChen
 *
 */
public class C3p0Helper {
	
	private static final String NAMED_CONFIG = "named-config";
	private static final String NAME = "name";
	
	private static String[] databaseNames;
	
	static{
		SAXReader saxReader = new SAXReader();
		try {
			InputStream inputStream = C3p0Helper.class.getClassLoader().getResourceAsStream("c3p0-config.xml");
			Document doc = saxReader.read(inputStream);
	        Element root = doc.getRootElement();
			
			List<?> childList = root.elements(NAMED_CONFIG);
			if(childList!=null && !childList.isEmpty()){
				databaseNames = new String[childList.size()];
				for(int i=0;i<childList.size();i++){
					Element namedConfig = (Element)childList.get(i);
					databaseNames[i] = namedConfig.attributeValue(NAME);
				}
			}
		} catch (DocumentException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 所有的数据源
	 */
	private Map<String, DataSource> dataSourceMap;
	
	/**
	 * 构造方法
	 */
	private C3p0Helper(){
		dataSourceMap = new HashMap<String, DataSource>();
		if(databaseNames!=null && databaseNames.length!=0){
			for(String database : databaseNames){
				dataSourceMap.put(database, new ComboPooledDataSource(database));
			}
		}
	}
	
	/**
	 * 单例
	 */
	private static C3p0Helper instance;
	
	/**
	 * 单例-获取C3p0DataBase实例
	 * @return
	 */
	public static synchronized C3p0Helper getInstance() {
		if(instance == null){
			instance = new C3p0Helper();
		}
		return instance;
	}

	/**
	 * 获取c3p0配置中named-config的name
	 * @param index
	 * @return
	 */
	public String getDataBaseName(int index){
		if(index>-1 && Assert.isValidArray(databaseNames)){
			return databaseNames[index];
		}else{
			return null;
		}
	}
	
	/**
	 * 获取连接
	 * @param database
	 * @return
	 */
	public synchronized Connection getConnection(String database){
		Connection connection = null;
		DataSource dataSource = dataSourceMap.get(database);
		if(dataSource != null){
			try {
				connection = dataSource.getConnection();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return connection;
    }
	
	/**
	 * 关闭连接
	 * @param conn
	 * @param st
	 * @param rs
	 */
	public synchronized void closes(Connection conn, Statement st, ResultSet rs){
		try {
			if(rs!=null){
				rs.close();
			}
			if(st!=null){
				st.close();
			}
			if(conn!=null){
				conn.close();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 关闭连接
	 * @param mysqlConn
	 * @param ps
	 */
	public synchronized void closes(Connection conn, Statement st) {
		closes(conn, st, null);
	}
	
	/**
	 * 关闭连接
	 * @param st
	 * @param rs
	 */
	public synchronized void closes(Statement st, ResultSet rs){
		closes(null, st, rs);
	}
	
	/**
	 * 关闭连接
	 * @param conn
	 */
	public synchronized void closes(Connection conn){
		closes(conn, null, null);
	}
	
	/**
	 * 关闭连接
	 * @param st
	 */
	public synchronized void closes(Statement st){
		closes(null, st, null);
	}
	
	/**
	 * 关闭连接
	 * @param rs
	 */
	public synchronized void closes(ResultSet rs){
		closes(null, null, rs);
	}
	
}
